
import AccApi from "../../common/api/food/statement.api.js";
import { DictionaryType, requestDict } from "../../common/dictionary/dictionary-type.js";


(function (window, document, $) {
    // 修改不同系统字体时html节点的font-size 尽量使rem计算得到的px相同
    var realfz = ~~(+window.getComputedStyle(document.getElementsByTagName("html")[0]).fontSize.replace('px', '') * 10000) / 10000
    let fontSize = 0
    let temp = 16
    if (realfz < 16) {
        temp = 14
    }
    if (realfz >= 16) {
        temp = 16
    }
    if (realfz >= 18) {
        temp = 17
    }
    if (realfz >= 20) {
        temp = 20
    }
    if (realfz >= 24) {
        temp = 24
    }
    if (realfz >= 26) {
        temp = 26
    }
    fontSize = (temp * 16 / realfz)
    document.getElementsByTagName("html")[0].style.cssText = `font-size: ${fontSize}px`
    // 使微信浏览器调整字体功能无效
    $(document).ready(() => {
        if (typeof WeixinJSBridge == "object" && typeof WeixinJSBridge.invoke == "function") {
            handleFontSize()
        } else {
            if (document.addEventListener) {
                document.addEventListener("WeixinJSBridgeReady", handleFontSize, false)
            } else if (document.attachEvent) {
                document.attachEvent("WeixinJSBridgeReady", handleFontSize)
                document.attachEvent("onWeixinJSBridgeReady", handleFontSize)
            }
        }
        function handleFontSize() {
            // 设置网页字体为默认大小
            WeixinJSBridge.invoke('setFontSizeCallback', { 'fontSize': 0 })
            // 重写设置网页字体大小的事件
            WeixinJSBridge.on('menu:setfont', function () {
                WeixinJSBridge.invoke('setFontSizeCallback', { 'fontSize': 0 })
            })
        }
    })

    new Vue({
        el: '#statement-app',
        data() {
            return {
                dataList: [],
                bizDate: this.getUrlKey('bizDate'),
                rcvableAmount: this.getUrlKey('rcvableAmount'),
                companyName:'',
                detailsList: [],
                isRemarkShow: false
            }
        },
        filters: {
            toThousands: (data) => {
                return Utils.toThousands(data)
            }
        },
        computed: {
            // 总计
            totalAmount({ dataList }) {
                let totalAmount = dataList.reduce((pre, cur) => {
                    let amount = cur.entryList.reduce((p, c) => {
                        return Math.round((Number(c.amount) + p) * 100) / 100
                    }, 0)
                    cur.amount = amount
                    return Math.round((amount + pre) * 100) / 100
                }, 0)
                return totalAmount
            },
            currentDateStr({ currentDate }) {
                return moment(currentDate).format('YYYY年MM月DD日')
            }
        },
        created() {
            // Utils.SetSNAuthAT('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVSWQiOiJvb2JnQiIsIlVOYW1lIjoi5p2O5rWpIiwiVVR5cGUiOiIxIiwiQ09VTmFtZSI6IuS6keWNl-elnuWGnOmbhuWboiIsIkNPVUlkIjoib29jYSIsIlNjb3BlIjoiSFRNTCIsIm5iZiI6MTY4MzE4MTM0NCwiZXhwIjoxNjgzMTg0OTQ0LCJpYXQiOjE2ODMxODEzNDQsImlzcyI6IioiLCJhdWQiOiIqIn0.zM_SjRtF1I_SzKTCNW46zgRY8vM5rH5HONPrv_ZLlHA')
            $(`#statement-app`).removeClass('d-none')
            let url = location.href
            let params = {} // url参数
            for (let key of (new URL(url)).searchParams.entries()) {
                key[1] ? params[key[0]] = key[1] : ''
            }
            this.currentDate = moment(params.date || '2023-06-07').format('YYYY-MM-DD')
            this.getStatementDate()
            // 点击其他地方时 隐藏弹窗
            document.addEventListener('click', (e) => {
                if (e.target.className.indexOf('wb-info-circle') === -1) {
                    this.resetShowRemark()
                }
            })
        },
        methods: {
            getStatementDate() {
                let bizDate = this.getUrlKey('bizDate')
                AccApi.postAccReceivablesList({
                    "sort":"bizFormObj.createTime desc",
                    "startDate":bizDate,
                    "endDate":bizDate,
                    "custType":"",
                    "riskLevel":"",
                    "directorName":"",
                    "manegerName":"",
                    "billStatus":null,
                    "billNo":"" 
                }).then(res => {
                    this.companyName = res.items[0].companyName;
                    let i = 0;
                    res.items.forEach(element => {
                        element.index = i++;
                    });
                    res.items = res.items.map(itm => {
                        return { ...itm, managerList: itm.manegerName?.split(',') || [] }
                    })
                    this.dataList = res.items;
                })
            },
            resetShowRemark() {
                this.dataList = this.dataList.map(item => {
                    item.showRemark = false
                    return item
                })
            },
            showRemark(index, idx) {
                if (this.dataList[index].showRemark) {
                    this.dataList[index].showRemark = false
                    this.$forceUpdate()
                } else {
                    this.resetShowRemark()
                    this.dataList[index].showRemark = true
                }
            },
            getUrlKey(name) {// 获取地址栏的参数
                var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
                var r = window.location.search.substr(1).match(reg);
                if (r != null) return unescape(r[2]); return null;
            },
            showDetails(e,id){
                $('.college-list-item').css('background','white');
                $(e.currentTarget.parentElement.parentElement).css('background','#eeeeee');
                AccApi.getAccreceivdailys(id).then(res => {
                    requestDict(DictionaryType.BillStatus).then(result => {
                        let billStatusInfo = {};
                        result.data.forEach(item => {
                            billStatusInfo[item.value] = item.displayName;
                        });
                        res.billStatus = billStatusInfo[res.bizFormObj.billStatus];
                        this.detailsList = [res];
                        $('#dialogWrap1').fadeIn(200);
                        $('#js_dialog_1').addClass('weui-half-screen-dialog_show');
                        setTimeout(function(){
                            $('#dialogWrap1').attr('aria-hidden','false');
                            $('#dialogWrap1').attr('aria-modal','true');
                            $('#dialogWrap1').attr('tabindex','0');
                            $('#dialogWrap1').trigger('focus');
                        },200)
                    })
                })
                
            },
            closeBtn(){
                const $jsDialogWrap = $('#dialogWrap1');
                $jsDialogWrap.attr('aria-hidden','true').attr('aria-modal','false').removeAttr('tabindex');
                $jsDialogWrap.fadeOut(300);
                $jsDialogWrap.find('.js_dialog').removeClass('weui-half-screen-dialog_show');
                setTimeout(function(){
                    $('#' + $jsDialogWrap.attr('ref')).trigger('focus');
                }, 300);
            },
        }
    })

    function closeDialog(o){
        const $jsDialogWrap = o.parents('.js_dialog_wrap');
        $jsDialogWrap.attr('aria-hidden','true').attr('aria-modal','false').removeAttr('tabindex');
        $jsDialogWrap.fadeOut(300);
        $jsDialogWrap.find('.js_dialog').removeClass('weui-half-screen-dialog_show');
        setTimeout(function(){
            $('#' + $jsDialogWrap.attr('ref')).trigger('focus');
        }, 300);
    }

    // 阻止弹窗内部滚动冒泡
    $('.js_dialog_wrap').on('touchmove', function(e) {
        if($.contains(document.getElementById('js_wrap_content'), e.target)){
        } else {
        e.preventDefault();
        }
    });

    $('.js_close').on('click', function() {
        closeDialog($(this));
    });


})(window, document, jQuery)